<?php
/**
 * 直接调拨
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/4/7
 * Time: 14:56
 */
namespace App\Http\Controllers\WarehouseManage;
use App\Http\Controllers\ApiController;
use App\Services\Warehouse\DirectTransfersService;
use Illuminate\Http\Request;

class DirectTransfersController extends ApiController{
    public $DirectTransfersService;
    public function __construct(DirectTransfersService $directTransfersService)
    {
        $this->DirectTransfersService=$directTransfersService;
    }

    /***
     * 退货仓直接调拨根据类型调拨指定仓库并提交审核
     * 参数说明
     * orderdata={"btype":1,"wid":8,"goodslist":[{"goods_sn":"6949552006549","goods_number":5,"spec_unit":"\u5305","k3_supplier_sn":"VEN00078","k3_vmi":1}]}
     * @param Request $request
    @author echa
     * @return array|bool|mixed
     */
    public function OrderGoodsBackToWarehouse(Request $request){
      $orderdata=$request->orderdata;
      if(empty($orderdata)){
          return $this->failed('非法请求');
      }
      $orderdata=json_decode($orderdata,true);
      if(empty($orderdata) || empty($orderdata['wid'])){
          return $this->failed('参数错误');
      }
      $data=[
          'wid'=>$orderdata['wid'],
          'note'=>$orderdata['note'],
          'goods'=>$orderdata['goodslist']
      ];
      try{
          $return=$this->DirectTransfersService->OrderGoodsBackToWarehouse($data,$orderdata['btype']);
          if($return){
              return $this->success($return);
          }else{
              return $this->failed('操作失败');
          }
      }catch (\Exception $exception){
          return $this->failed($exception->getMessage());
      }
    }

    /***
     * 主仓操作次品录入直接调拨到次品仓
     * 参数说明
     * rdata={"wid":8,"goodslist":[{"goods_sn":"6902890234166","goods_number":1,"spec_unit":"条","k3_supplier_sn":"","k3_vmi":0}]}
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function DefectiveGoodsEntry(Request $request){
        $rdata=$request->rdata;
        if(empty($rdata)){
            return $this->failed('非法请求');
        }
        $rdata=json_decode($rdata,true);
        if(empty($rdata) || empty($rdata['wid'])){
            return $this->failed('参数错误');
        }
        $data=[
            'wid'=>$rdata['wid'],
            'note'=>$rdata['note'],
            'goods'=>$rdata['goodslist']
        ];
        try{
            $return=$this->DirectTransfersService->DefectiveGoodsEntry($data);
            if($return){
                return $this->success($return);
            }else{
                return $this->failed('操作失败');
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }
    }
}